package com.xteam.ecsystem.contorller;

import java.io.IOException;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import com.tangkf.utils.bean.ReqHelper;
import com.xteam.ecsystem.entities.BaseReplyInfo;
import com.xteam.ecsystem.service.admin.MemberCommentsService;
import com.xteam.ecsystem.service.admin.SystemLogService;
import com.xteam.ecsystem.utils.ConstantLocation;

@Controller
public class MemberCommentsContorller {
	
	@Autowired
	private MemberCommentsService memberCommentsService;
	
	@Autowired
	private SystemLogService logService;

	@RequestMapping("/findMemberComments.do")
	public void findMemberComments(HttpServletRequest request,
			HttpServletResponse response, ModelMap modelMap) throws IOException {
		int pageIndex = ReqHelper.getRequestInt(request, "pageIndex");
		int pageSize = ReqHelper.getRequestInt(request, "pageSize");
		String bucName = ReqHelper.getRequestString(request, "bucName");
		String bucType = ReqHelper.getRequestString(request, "bucType");
		String sbucTime = ReqHelper.getRequestString(request, "sbucTime");
		String ebucTime = ReqHelper.getRequestString(request, "ebucTime");
		String bucState = ReqHelper.getRequestString(request, "bucState");
		String strList = memberCommentsService.getMemberCommentsList(pageIndex,
				pageSize, bucName, bucType, sbucTime, ebucTime, bucState);
		response.getWriter().write(strList);
	}
	
	@RequestMapping("/findCommentsById.do")
	public void findCommentsById(HttpServletRequest request,
			HttpServletResponse response) throws IOException {
		String id = ReqHelper.getRequestString(request, "id");
		String strList = memberCommentsService.findCommentsById(id);
		response.getWriter().write(strList);
	}
	
	@RequestMapping("/delComments.do")
	public void delComments(HttpServletRequest request,
			HttpServletResponse response, ModelMap modelMap) throws IOException {
		String bucId = ReqHelper.getRequestString(request, "bucId");
		String lslUser = ReqHelper.getRequestString(request, "lslUser");
		String lslAddress = ReqHelper.getRequestString(request, "lslAddress");
		
		memberCommentsService.delComments(bucId);
		
		this.logService.saveSystemLogs(lslUser, lslAddress, ConstantLocation.LOG_MEMBERCOM_MGR, 
				"delComments", ConstantLocation.LOG_DELETE_RES, "1-BaseUserComment，2-BaseReplyInfo");
	}
	
	 

	@RequestMapping("/findCommentsReply.do")
	public void findCommentsReply(HttpServletRequest request,
			HttpServletResponse response, ModelMap modelMap) throws IOException {
		String briTarget= ReqHelper.getRequestString(request, "briTarget");
		String strList = memberCommentsService.getCommentsReply(briTarget);
		response.getWriter().write(strList);
	}

	@RequestMapping("/auditComments.do")
	public void auditComments(HttpServletRequest request,
			HttpServletResponse response, ModelMap modelMap) throws IOException {
		String bucId = ReqHelper.getRequestString(request, "bucId");
		String bumState = ReqHelper.getRequestString(request, "bumState");
		String lslUser = ReqHelper.getRequestString(request, "lslUser");
		String lslAddress = ReqHelper.getRequestString(request, "lslAddress");
		
		memberCommentsService.updateComments(bucId, bumState);
		
		this.logService.saveSystemLogs(lslUser, lslAddress, ConstantLocation.LOG_MEMBERCOM_MGR, 
				"updateComments", ConstantLocation.LOG_Edit_STATE, "BaseUserComment");
	}
	
	@RequestMapping("/saveOrUpdateComments.do")
	public void saveOrUpdateComments(HttpServletRequest request,
			HttpServletResponse response, ModelMap modelMap) throws IOException {
		String jsonStr = ReqHelper.getRequestString(request, "CommentsInfo");
		String lslUser = ReqHelper.getRequestString(request, "lslUser");
		String lslAddress = ReqHelper.getRequestString(request, "lslAddress");
		
		JSONObject obj = JSONObject.fromObject(jsonStr);
		BaseReplyInfo baseReplyInfo = (BaseReplyInfo) JSONObject.toBean(obj,
				BaseReplyInfo.class);
		
		baseReplyInfo.setBriIp(lslAddress);
		baseReplyInfo.setBriTime(new Date());
		
		memberCommentsService.saveOrUpdateComments(baseReplyInfo,baseReplyInfo.getBriId());
		
		this.logService.saveSystemLogs(lslUser, lslAddress, ConstantLocation.LOG_MEMBERCOM_MGR, 
				"saveOrUpdateComments", ConstantLocation.LOG_REPLYCOMMS, "BaseReplyInfo");
		
		JSONObject obj1 = new JSONObject();
		obj1 = JSONObject.fromObject(baseReplyInfo);
		
		response.getWriter().write(obj1.toString());
	}
}
